//
//  UIDevice+EV.m
//  EVClub
//
//  Created by sam on 14-9-3.
//  Copyright (c) 2014年 BitRice. All rights reserved.
//

#import "UIDevice+EV.h"
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/utsname.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CoreTelephonyDefines.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

static double kNormal_spanDelta_6 = 0.008989516619413206;
static double kNormal_spanDelta_5s = 0.0074655563357432005;
static double kNormal_spanDelta_4s = 0.0067047845180080393;
static double kNormal_spanDelta_6p = 0.0099543540362372583;

@implementation UIDevice (EV)

+ (CGFloat)ev_iosVersion
{
    static CGFloat version = 0.0f;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray* verArray = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
        NSInteger j = 0;
        for (NSInteger i = 0; i < verArray.count; ++i) {
            CGFloat val = [verArray[i] floatValue];
            if (i == 0) {
                version = val;
                continue ;
            }
            j += (val?floorf(log10f(val)):0)+1;
            version += val/powf(10, j);
        }
    });
    return version;
}

+ (NSString*)ev_deviceModel {
    return [UIDevice currentDevice].model;
}

+(NSString*)ev_hardwareType
{
    static NSString* hardwareType;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        struct utsname uDevice;
        uname (&uDevice);
        hardwareType = @(uDevice.machine);
    });
    return hardwareType;
}

+ (NSString*)ev_iosVersionString
{
    return [UIDevice currentDevice].systemVersion;
}

+ (CGFloat)ev_iosShortVersion
{
    static CGFloat version = 0.0f;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        version = [[[UIDevice currentDevice] systemVersion] floatValue];
    });
    return version;
}

+ (BOOL)ev_isIPad
{
    static BOOL isIPad;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        isIPad = ([[UIDevice currentDevice] userInterfaceIdiom]
                  == UIUserInterfaceIdiomPad);
    });
    return isIPad;
}

+(BOOL)ev_isIphone_667
{
    static BOOL value;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        value = (fabs([[UIScreen mainScreen] bounds].size.height - (double)667)<DBL_EPSILON)?YES:NO;
    });
    return value;
}

+(BOOL)ev_isIphone_568
{
    static BOOL value;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        value = (fabs((double)[[UIScreen mainScreen] bounds].size.height-(double)568)<DBL_EPSILON)?YES:NO;
    });
    return value;
}

+ (BOOL)ev_isIphone_480
{
    static BOOL value;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        value = (fabs((double)[[UIScreen mainScreen] bounds].size.height-(double)480)<DBL_EPSILON)?YES:NO;
    });
    return value;
}

+(BOOL)ev_isIphone_736
{
    static BOOL value;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        value = (fabs((double)[[UIScreen mainScreen] bounds].size.height-(double)736)<DBL_EPSILON)?YES:NO;
    });
    return value;
}

+ (BOOL)ev_isIPod
{
    static BOOL isIPod;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        isIPod = [[[self class] ev_deviceModel] rangeOfString:@"iPod"].location != NSNotFound;
    });
    return isIPod;
}
@end
